package com.zzgele.qmzb.api.utils;

import com.zzgele.qmzb.api.core.Constants;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Martin on 2017-07-18.
 */
public class UserUtil {
    /**
     * 生成IM所用的accid
     * @return
     */
    static  String   pattern = "([\\d])\\1{2,}";
    static Pattern pa = Pattern.compile(pattern);
    static  String   pattern2 = "(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){2,}|(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){2,})\\d";
    static Pattern pa2 = Pattern.compile(pattern2);

    public static String getImAccid(){
        return UUIDUtil.getUUID();
    }

    /**
     * 根据手机号获取昵称
     * @param mobile
     * @return
     */
    public static String getNickNameByMobile(String mobile) {
        return StringUtil.getMobileFormat(mobile) + "_" + StringUtil.getRandomFour();
    }

    public static String getNickNameById(Long id) {
        return Constants.USER_NICKNAME_PREFIX_DEF.concat(id.toString());
    }
    public static void main(String[] args) {
//        for (int i = 1; i < 31; i++) {
//            initData(i);
//        }
        getNumberById(1234L,0L);
    }
    public static Long getNumberById(Long id,Long max) {
        if(max==null||max< Constants.LOGIN_NUMBER_BASE ) {
            max = Constants.LOGIN_NUMBER_BASE + id;
        }
         //排除连号
        for(int i=1;i<10000;i++) {
            Long number = max+i;
            
            Matcher ma = pa.matcher(number.toString());
            if (ma.find()) {
                continue;
            }
            Matcher ma2 = pa2.matcher(number.toString());
            if (ma.find()) {
                continue;
            }
            return number;
        }
        return 0L;
//        String numberString = number.toString().concat("4");
//        return Long.valueOf(numberString);
    }

    /**
     * 过滤昵称特殊表情
     */
    public static String filterName(String name) {
        if(name==null){
            return "";
        }
        if(StringUtil.isEmpty(name)){
            return "";
        }
        name = EmojiUtil.filter(name);

        Pattern patter = Pattern.compile("[a-zA-Z0-9\u4e00-\u9fa5]");
        Matcher match = patter.matcher(name);

        StringBuffer buffer = new StringBuffer();

        while (match.find()) {
            buffer.append(match.group());
        }
        return buffer.toString();
    }

    /*//获取用户ID
    public static Long getLoginUidByUserId(Long uid) {
        return Constants.LOGIN_UID_BASE + uid;
    }

    //获取用户真实ID
    public static Long getUserIdByLoginUid(Long login_uid) {
        Long userId = login_uid - Constants.LOGIN_UID_BASE;
        return userId < 0 ? 0 : userId;
    }*/
}
